package jou.cs.configurator;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

@Configuration @EnableAsync @Data
@ConfigurationProperties(prefix = "spring.async")
public class async {
    private Integer max;
    private Integer core;
    private Integer queue;
    private Integer seconds;
    private String prefix;

    @Bean
    public ThreadPoolTaskExecutor threadPoolTaskExecutor()  {
        ThreadPoolTaskExecutor task = new ThreadPoolTaskExecutor();

        task.setCorePoolSize(core);
        task.setMaxPoolSize(max);
        task.setQueueCapacity(queue);
        task.setThreadNamePrefix(prefix);
        task.setKeepAliveSeconds(seconds);
        task.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());

        return task;
    }
}
